package cn.xawl.gulimall.ware.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.xawl.gulimall.ware.vo.PurchaseDoneVo;
import cn.xawl.gulimall.ware.vo.PurchaseMergeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.xawl.gulimall.ware.entity.PurchaseEntity;
import cn.xawl.gulimall.ware.service.PurchaseService;
import cn.xawl.common.utils.PageUtils;
import cn.xawl.common.utils.R;



/**
 * 采购信息
 *
 * @author lh
 * @email lhm49869999@163.com
 * @date 2020-06-17 19:43:36
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    /**
     * /ware/purchase/unreceive/list
     * 查询未领取的订单列表
     */
    @RequestMapping("/unreceive/list")
    public R unReceiveList(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.queryUnReceiveList(params);

        return R.ok().put("page", page);
    }

    /**
     * /ware/purchase/merge
     * 将一个或多个采购列表合并为一个采购单
     */
    @RequestMapping("/merge")
    public R merge(@RequestBody PurchaseMergeVo purchaseMergeVo) {
        purchaseService.mergePurchaseDetail(purchaseMergeVo);
        return R.ok();
    }

    /**
     * /ware/purchase/received
     * 采购人员领取采购单
     */
    @RequestMapping("/received")
    public R received(@RequestBody List<Long> items) {
        purchaseService.receivePurchase(items);
        return R.ok();
    }

    /**
     * /ware/purchase/done
     * 采购人员完成采购
     */
    @RequestMapping("/done")
    public R done(@RequestBody PurchaseDoneVo purchaseDoneVo) {
        purchaseService.purchaseDone(purchaseDoneVo);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
        public R save(@RequestBody PurchaseEntity purchase){
        purchase.setCreateTime(new Date());
        purchase.setUpdateTime(new Date());
		purchaseService.save(purchase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
        public R update(@RequestBody PurchaseEntity purchase){
        purchase.setUpdateTime(new Date());
		purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
        public R delete(@RequestBody Long[] ids){
		purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
